package org.datascooter.test.example;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.persistence.Transient;

import org.datascooter.impl.Data;
import org.datascooter.utils.ReferenceInvoker;

public class Trade extends Data {// implements IFlushable {

	private static final long serialVersionUID = -7955079376770352199L;
	String name;
	Integer type;
	String goods;
	Long price;
	Date start;
	Date stop;
	String userId;

	public Trade() {
	}

	public Object[] flush() {
		return new Object[] {
				getId(), name, type, goods, price, start, stop, userId
		};
	}

	public void restore(Object[] fields) throws Exception {
		id = fields[0] + "";
		goods = fields[3] + "";
		name = fields[1] + "";
		price = new Long(fields[4] + "");
		type = new Integer(fields[2] + "");
		start = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(fields[5] + "");
		stop = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(fields[6] + "");
		userId = fields[7] + "";
	}

	public Trade(String name, String goods, Long price, Date start, Date stop, String userId) {
		this.name = name;
		this.goods = goods;
		this.price = price;
		this.start = start;
		this.stop = stop;
		this.userId = userId;
		this.type = 0;
	}

	@Transient
	public NaturalUser getUser() {
		try {
			return ReferenceInvoker.invoke(this, "getUser");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Transient
	public List<Offer> getOfferList() {
		try {
			return (List) ReferenceInvoker.invokeCollection(this, "getOfferList");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Transient
	public List<Offer> getContractList() {
		try {
			return (List) ReferenceInvoker.invokeCollection(this, "getContractList");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Transient
	@Override
	public String toString() {
		return name + " - " + goods + " - " + price;
	}

	public String getName() {
		return name;
	}

	public String getGoods() {
		return goods;
	}

	public Long getPrice() {
		return price;
	}

	public Date getStart() {
		return start;
	}

	public Date getStop() {
		return stop;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setGoods(String goods) {
		this.goods = goods;
	}

	public void setPrice(Long price) {
		this.price = price;
	}

	public void setStart(Date start) {
		this.start = start;
	}

	public void setStop(Date stop) {
		this.stop = stop;
	}

	public Integer getType() {
		return type;
	}

	public void setType(Integer type) {
		this.type = type;
	}
}
